package com.gla.controller;

import com.alibaba.fastjson.JSON;
import com.gla.dao.UserDao;
import com.gla.entity.QueryInfo;
import com.gla.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserDao userDao;

    @GetMapping("/getUser")
    public String getUserList(QueryInfo queryInfo){
        int numbers = userDao.getUserCounts("%"+queryInfo.getQuery()+"%");// 获取用户总数
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();// 开始页面
        List<User> users = userDao.getAllUser("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",users);
        System.out.println("用户数量："+numbers);
        String users_json = JSON.toJSONString(res);
        return users_json;
    }


    @GetMapping("/addUser")
    public String addUser(User user){
        System.out.println(user);
        user.setPhoto("http://101.33.206.254:88/images/2022/07/03/QQ20220704015945.png");
//        user.setState(false);
        user.setIsdel(0);
        int i = userDao.addUser(user);
        String str = i >0?"success":"error";
        return str;
    }

    @GetMapping("/deleteUser")
    public String deleteUser(int id){
        System.out.println(id);
        int i = userDao.deleteUser(id);
        String str = i >0?"success":"error";
        return str;
    }

    @GetMapping("/getUpdate")
    public String getUpdateUser(int id){
        System.out.println("编号:"+id);
        User updateUser = userDao.getUpdateUser(id);
        String users_json = JSON.toJSONString(updateUser);
        return users_json;
    }

    @GetMapping("/editUser")
    public String editUser(User user){
        System.out.println(user);
        int i = userDao.editUser(user);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/userState")
    public String updateUserState(Integer  id,
                                  Boolean is_vip){
        int i = userDao.updateState(id,is_vip);
        System.out.println("用户编号:"+id);
        System.out.println("用户状态:"+is_vip);
        String str = i >0?"success":"error";
        return str;
    }


}
